﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Interactivity;

using Telerik.Windows.Controls;

namespace Rio.Manager.Application.Behaviors
{
    public class ValidationDatesBehavior : Behavior<Button>
    {
        #region Fields

        public static readonly DependencyProperty EndRadDatePickerProperty = 
            DependencyProperty.Register("EndRadDatePicker", typeof(RadDatePicker), typeof(ValidationDatesBehavior), null);
        public static readonly DependencyProperty StartRadDatePickerProperty = 
            DependencyProperty.Register("StartRadDatePicker", typeof(RadDatePicker), typeof(ValidationDatesBehavior), null);

        #endregion Fields

        #region Constructors

        public ValidationDatesBehavior()
            : base()
        {
        }

        #endregion Constructors

        #region Properties

        public RadDatePicker EndRadDatePicker
        {
            get { return (RadDatePicker)GetValue(EndRadDatePickerProperty); }
            set { SetValue(EndRadDatePickerProperty, value); }
        }

        public RadDatePicker StartRadDatePicker
        {
            get { return (RadDatePicker)GetValue(StartRadDatePickerProperty); }
            set { SetValue(StartRadDatePickerProperty, value); }
        }

        #endregion Properties

        #region Methods

        /// <summary>
        /// Called when [attached].
        /// </summary>
        protected override void OnAttached()
        {
            base.OnAttached();
            this.AssociatedObject.Click += AssociatedObject_Click;
        }

        /// <summary>
        /// Called when [detaching].
        /// </summary>
        protected override void OnDetaching()
        {
            base.OnDetaching();
            this.AssociatedObject.Click -= AssociatedObject_Click;
        }

        /// <summary>
        /// Handles the Click event of the AssociatedObject control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void AssociatedObject_Click(object sender, RoutedEventArgs e)
        {
            UpdateBindingExpression(StartRadDatePicker, RadDatePicker.SelectedValueProperty);
            UpdateBindingExpression(EndRadDatePicker, RadDatePicker.SelectedValueProperty);
        }

        /// <summary>
        /// Updates the binding expression.
        /// </summary>
        /// <param name="obj">The obj.</param>
        /// <param name="prop">The prop.</param>
        private void UpdateBindingExpression(DependencyObject obj, DependencyProperty prop)
        {
            BindingExpression L_beBinding = obj.ReadLocalValue(prop) as BindingExpression;
            L_beBinding.UpdateSource();
        }

        #endregion Methods
    }
}